import { AddData, SavedData } from "./data";
import settings from "./settings";
import { RemoveEmoji } from "./utils";
import { HypixelModAPI } from "../../HypixelModAPI";

//location
export let location = {
    day : -1,
    inLimbo : false,
    inSkyblock : false,
    inDungeon : false,
    inKuudra : false,
    location : "",
    island : "",
    server : "",
    volcano : "",
    profileName : "",
    profileID : "",
    SBProfile : "",
    game : "",
    mode : "",
    map : "",

    registerProfileSwitchTrigger(trigger) {
        profileSwitchTriggers.push(trigger);
    }
}
let profileSwitchTriggers = [];

//get location

//来自HypixelModAPI的位置信息
HypixelModAPI.on("location", (loc) => {
    location.server = (loc.serverName === null ? "" : loc.serverName);
    location.inLimbo = loc.serverName === "limbo";
    location.game = (loc.serverType === null ? "" : loc.serverType);
    location.mode = (loc.mode === null ? "" : loc.mode);
    location.map = (loc.map === null ? "" : loc.map);
    location.inSkyblock = loc.serverType === "SKYBLOCK";
    location.inDungeon = loc.serverType === "SKYBLOCK" && loc.map === "Dungeon";
    location.inKuudra = loc.serverType === "SKYBLOCK" && loc.map === "Kuudra";
    location.island = (loc.serverType === "SKYBLOCK" ? loc.map : "");
})

//来自计分板和Tab的位置信息补充
register("tick",()=>{
    location.day = Math.floor(World.getTime()/20/60/20 * 100) / 100;
    if (
        Scoreboard.getLines()[Scoreboard.getLines().length-1] != undefined && location.inSkyblock
    ) {
        let locationOnScoreborad = Scoreboard.getLines().filter(e => e.getName().includes("⏣") || e.getName().includes("ф"));
        if(locationOnScoreborad != 0) location.location = RemoveEmoji(locationOnScoreborad[0].getName().removeFormatting().replace(" ⏣ ","").replace(" ф ",""));
        TabList.getNames().forEach(e => {
            switch (true) {
                case e.removeFormatting().startsWith(" Volcano: "):
                    location.volcano = e.replace(" Volcano: ","");
                    volcano = true;
                    break;
                case e.removeFormatting().startsWith("Profile: "):
                    location.profileName = e.removeFormatting().replace("Profile: ","");
                    if(location.island == "The Rift") location.profileName = location.profileName.split('').reverse().join('');
                    break;
            }
        })
    }
})

//获取存档id
AddData("SkyblockProfileID","");
location.profileID = SavedData.SkyblockProfileID;
location.SBProfile = Player.getUUID() + '_' + location.profileID;
register("chat", (id,event) => {
    if(!location.inSkyblock) return;
    location.profileID = id;
    SavedData.SkyblockProfileID = id;
    location.SBProfile = Player.getUUID() + '_' + location.profileID;
    profileSwitchTriggers.forEach(trigger => trigger(location.SBProfile));
}).setCriteria("Profile ID: ${id}")

//加载世界时重置位置
register("worldLoad",()=>{
    location.day = -1;
    location.location = "";
    location.volcano = "";
})

//离开服务器时重置
register("serverDisconnect", () => {
    location.day = -1;
    location.inSkyblock = false;
    location.inDungeon = false;
    location.inKuudra = false;
    location.location = "";
    location.island = "";
    location.server = "";
    location.volcano = "";
    location.game = "";
    location.mode = "";
    location.map = "";
})

//重载模块时获取位置
try {
    if (
        Scoreboard.getLines()[Scoreboard.getLines().length-1] != undefined && 
        (ChatLib.removeFormatting(Scoreboard.getScoreboardTitle().toString()).includes("SKYBLOCK") || ChatLib.removeFormatting(Scoreboard.getScoreboardTitle().toString()).includes("SKIBLOCK"))
    ) {
        location.inSkyblock = true;
        for (let i = Scoreboard.getLines().length - 1; i >= 0; i--) {
            if(
                Scoreboard.getLineByIndex(parseInt(i)).toString().includes("⏣") ||
                Scoreboard.getLineByIndex(parseInt(i)).toString().includes("ф")
            ){
                let locat = ChatLib.removeFormatting(Scoreboard.getLineByIndex(i).toString()).replace(" ⏣ ","").replace(" ф ","");
                location.location = RemoveEmoji(locat);
            }
        }
        TabList.getNames().forEach((e)=>{
            if(e.includes("Area: ")){
                location.island = ChatLib.removeFormatting(e).replace("Area: ","");
                if(location.island == "Kuudra"){
                    location.inKuudra = true;
                    location.inDungeon = false;
                }else{location.inKuudra = false;}
            }else if(e.includes("Dungeon: ")){
                location.inDungeon = true;
                location.inKuudra = false;
                location.island = ChatLib.removeFormatting(e).replace("Dungeon: ","");
            }else if(e.includes(" Server: ")){
                location.server = ChatLib.removeFormatting(e).replace(" Server: ","");
            }
        })
    }
} catch (error) {}